package com.smshen;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * ClassName: 类名称
 *
 * @Description: 描述
 * @Author wangxianwang
 * @CreatDate 2019.12.23
 */

public class ExportExcel {
    public static void main(String[] args) throws Exception{
        List<String[]> list = new ArrayList<>();
        list.add(new String[]{"系统管理","null","1","2"});
        list.add(new String[]{"人员管理","null","1","2"});
        ExportExcel exportExcel = new ExportExcel();
        OutputStream outputStream = new FileOutputStream("H:\\export.xls");
        exportExcel.ExportExcel(list,outputStream);
        outputStream.flush();
        outputStream.close();

}
    public void ExportExcel(List<String[]> resource, OutputStream outputStream){

        //创建一个内存Excel对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("sheetl");
        //创建表头
        String[] headerStr = resource.get(0);
        HSSFRow headerRow = sheet.createRow(0);
        //设置列宽
        for (int i=0; i < headerStr.length;i++){
            sheet.setColumnWidth(i,5000);

        }
        //设置头单元格样式
        HSSFCellStyle headerStyle = workbook.createCellStyle();
//        headerStyle.setAlignment(HSSFCellStyle);
//设置字体
        HSSFFont headerFont = workbook.createFont();
//        headerFont.setColor(HSSFColor.)
      headerFont.setFontName("宋体");
      headerStyle.setFont(headerFont);
//定义表头
      for (int i=0; i<headerStr.length;i++){
         //创建一个单元格
         HSSFCell headerCell= headerRow.createCell(i);
         headerCell.setCellStyle(headerStyle);
         headerCell.setCellValue(headerStr[i]);
      }
      //定义表体内容
        //样式
        //设置头单元格样式
        HSSFCellStyle bodyStyle = workbook.createCellStyle();
//        headerStyle.setAlignment(HSSFCellStyle);
//设置字体
        HSSFFont bodyFont = workbook.createFont();
//        headerFont.setColor(HSSFColor.)
        bodyFont.setFontName("微雅软黑");
        bodyStyle.setFont(bodyFont);

        for (int row=1;row<resource.size(); row++){
            //输入的行数据
            String[] temp = resource.get(row);
            //创建行
            HSSFRow bodyRow = sheet.createRow(row);
            //循环创建列
            for (int cell = 0;cell < temp.length;cell++){
                HSSFCell bodyCell = bodyRow.createCell(cell);
                bodyCell.setCellStyle(bodyStyle);
                bodyCell.setCellValue(temp[cell]);
            }
        }
        //将内存创建的Excel，输出到文件里
        try {
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
